package com.lv.LvTourismForSale.web;
import com.aqgj.User.bean.User;
import com.lv.LvTourismForSale.bean.LvTourismForSale;
import com.lv.LvTourismForSale.service.LvTourismForSaleManager;
import com.lv.LvTourismModel.bean.LvTourismModel;
import com.lv.LvTourismModel.service.LvTourismModelManager;
import com.mysql.fabric.Server;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("lvTourismForSaleAction")
public class LvTourismForSaleAction {
    @Autowired
    private LvTourismForSaleManager lvTourismForSaleManager;
    @Autowired
    private ZtcTransportManager ztcTransportManager;
    private static final Logger log = LogManager.getLogger(LvTourismModel.class);

    /**
     * 修改
     * @param lvTourismForSale 在售旅游对象
     * @param priceComboData 价格套餐json字符串
     * @param session
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public HashMap<String, Object> update(LvTourismForSale lvTourismForSale, String priceComboData,HttpSession session){
        log.info("开始修改");
        HashMap<String, Object> wapper = new HashMap<>();
        User user = (User) session.getAttribute("userInfo");
        if (user == null || "".equals(user)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "用户未登录");
            log.info("用户未登录");
            return wapper;
        }
        ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
        int transportType = transport.getType();
        if (transportType == 0 || transportType == 1 || transportType == 2) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "非旅游公司不可使用此功能");
            log.info("非旅游公司不可使用此功能");
            return wapper;
        }
        if (transport.getStatus() == 4) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "该用户公司已被冻结，不可使用此功能");
            log.info("该用户公司已被冻结，不可使用此功能");
            return wapper;
        }
        String result=lvTourismForSaleManager.update(lvTourismForSale,priceComboData,user);
        if("0".equals(result)){
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "修改失败");
            log.info("修改失败");
            return wapper;
        }
        wapper.put("success", Boolean.valueOf(true));
        wapper.put("data", "修改成功");
        log.info("修改成功");
        return wapper;
    }

    /**
     * 获取
     * @param id 在售旅游id
     * @param session
     * @return
     */
    @RequestMapping("get")
    @ResponseBody
    public HashMap<String, Object> get(String id, HttpSession session){
        HashMap<String, Object> wapper = new HashMap<>();
        User user = (User) session.getAttribute("userInfo");
        if (user == null || "".equals(user)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "用户未登录");
            log.info("用户未登录");
            return wapper;
        }
        ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
        int transportType = transport.getType();
        if (transportType == 0 || transportType == 1 || transportType == 2) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "非旅游公司不可使用此功能");
            log.info("非旅游公司不可使用此功能");
            return wapper;
        }
        if (transport.getStatus() == 4) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "该用户公司已被冻结，不可使用此功能");
            log.info("该用户公司已被冻结，不可使用此功能");
            return wapper;
        }
        HashMap<String,Object> result=lvTourismForSaleManager.get(id);
        wapper.put("success", Boolean.valueOf(true));
        wapper.put("lvTourismForSale", result.get("lvTourismForSale"));
        wapper.put("priceComboData", result.get("priceComboData"));
       /* wapper.put("pictureData", result.get("pictureData"));
        wapper.put("attachmentData", result.get("attachmentData"));*/
        return wapper;
    }

    /**
     * 获取在售旅游列表
     * @param pageNumber 第几页
     * @param pageSize 每页大小
     * @param startSiteName 起始站点名称
     * @param terminalSiteName 结束站点名称
     * @param status 状态：0.在售1.停售2.起草
     * @param session
     * @return
     */
    @RequestMapping("getList")
    @ResponseBody
    public HashMap<String, Object> getList(String pageNumber,String pageSize,String startSiteName,String terminalSiteName, String status,HttpSession session){
        HashMap<String, Object> wapper = new HashMap<>();
        User user = (User) session.getAttribute("userInfo");
        if (user == null || "".equals(user)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "用户未登录");
            log.info("用户未登录");
            return wapper;
        }
        ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
        int transportType = transport.getType();
        if (transportType == 0 || transportType == 1 || transportType == 2) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "非旅游公司不可使用此功能");
            log.info("非旅游公司不可使用此功能");
            return wapper;
        }
        if (transport.getStatus() == 4) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "该用户公司已被冻结，不可使用此功能");
            log.info("该用户公司已被冻结，不可使用此功能");
            return wapper;
        }
        List<LvTourismForSale> list=lvTourismForSaleManager.getList(pageNumber,pageSize,startSiteName,terminalSiteName,status);
        wapper.put("success", Boolean.valueOf(true));
        wapper.put("list",list);
        return wapper;
    }
    /**
     * 停售班次模板及其所有的在线班次
     * @param id 班次模板id
     * @param session
     * @return 操作结果信息
     * @throws ParseException
     */
    @RequestMapping("stopSell")
    @ResponseBody
    public HashMap<String, Object> stopSell(String id, HttpSession session) throws ParseException{
        Date now = new Date();
        HashMap<String, Object> wapper = new HashMap<>();
        User user = (User) session.getAttribute("userInfo");
        if (user == null || "".equals(user)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "用户未登录");
            log.info("用户未登录");
            return wapper;
        }
        ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
        int transportType = transport.getType();
        if (transportType == 0 || transportType == 1 || transportType == 2) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "非旅游公司不可使用此功能");
            log.info("非旅游公司不可使用此功能");
            return wapper;
        }
        if (transport.getStatus() == 4) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "该用户公司已被冻结，不可使用此功能");
            log.info("该用户公司已被冻结，不可使用此功能");
            return wapper;
        }
        LvTourismForSale lvTourismForSale=lvTourismForSaleManager.getById(id);
        if (lvTourismForSale.getStatus() == 1) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "该旅游产品已停售");
            return wapper;
        }
        lvTourismForSale.setStatus(1);
        lvTourismForSale.setFkModifyUserId(user.getId());
        lvTourismForSale.setModifyTime(now);
        String result = lvTourismForSaleManager.stopSell(lvTourismForSale);
        if("overEndDate".equals(result)){
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "此在售旅游产品服务日期已超过自动排班结束日期");
            log.info("此在售旅游产品服务日期已超过自动排班结束日期");
            return wapper;
        }
        if ("0".equals(result)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "停售失败");
            return wapper;
        }
        wapper.put("success", Boolean.valueOf(true));
        wapper.put("data", "停售成功");
        return wapper;
    }

    /**
     * 起售班次模板及其所有的停售班次
     * @param id 班次模板id
     * @param session
     * @return 操作结果信息
     */
    @RequestMapping("restartToSell")
    @ResponseBody
    public HashMap<String,Object> restartToSell(String id,String modelId, String serverDate,HttpSession session){
        Date now = new Date();
        HashMap<String, Object> wapper = new HashMap<>();
        User user = (User) session.getAttribute("userInfo");
        if (user == null || "".equals(user)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "用户未登录");
            log.info("用户未登录");
            return wapper;
        }
        ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
        int transportType = transport.getType();
        if (transportType == 0 || transportType == 1 || transportType == 2) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "非旅游公司不可使用此功能");
            log.info("非旅游公司不可使用此功能");
            return wapper;
        }
        if (transport.getStatus() == 4) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "该用户公司已被冻结，不可使用此功能");
            log.info("该用户公司已被冻结，不可使用此功能");
            return wapper;
        }
        if(id != null && !"".equals(id)){
            LvTourismForSale lvTourismForSale=lvTourismForSaleManager.getById(id);
            if (lvTourismForSale.getStatus() == 0) {
                wapper.put("success", Boolean.valueOf(false));
                wapper.put("msg", "该旅游产品已起售");
                return wapper;
            }
        }
        String result = lvTourismForSaleManager.restartToSell(id,modelId,serverDate,user.getId());
        if("overEndDate".equals(result)){
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "此在售旅游产品服务日期已超过自动排班结束日期");
            log.info("此在售旅游产品服务日期已超过自动排班结束日期");
            return wapper;
        }
        if ("0".equals(result)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "起售失败");
            return wapper;
        }
        wapper.put("success", Boolean.valueOf(true));
        wapper.put("data", "起售成功");
        return wapper;
    }
}